﻿using System;
using System.Collections.Generic;
using NHibernate.Cfg;
using nPress.Core.Entities;
using nPress.Core.Repositories;
using NUnit.Framework;

namespace nPress.Tests.Models
{
    /// <summary>
    /// Summary description for WordPressTest
    /// </summary>
    [TestFixture]
    public class WordPressTest
    {
        private NHibernateRepository _repository;

        [TestFixtureSetUp]
        public void Init()
        {
            Configuration cfg = new Configuration();
            cfg.Configure("WordPress.cfg.xml");
            _repository = new NHibernateRepository(cfg);
        }

        [TestFixtureTearDown]
        public void Dispose()
        {
            _repository.Dispose();
        }

        [Test]
        public void GetDefaultComment()
        {
            // Arrange
            string text;
            IList<Comment> comments;

            // Act
            comments = _repository.FindAll<Comment>();

            // Assert
            Assert.AreEqual(1, comments.Count, "1 comment after clean install");

            if (comments.Count == 1)
            {
                Comment c = comments[0];
                Assert.AreEqual(1, c.Id, "Comment's id");
                //Assert.AreEqual(); // post relation
                Assert.AreEqual("Monsieur WordPress", c.Author, "Comment's author");
                Assert.AreEqual(string.Empty, c.AuthorEmail, "Comment's author email");
                Assert.AreEqual("http://wordpress.org/", c.AuthorUrl, "Comment's author url");
                Assert.AreEqual(string.Empty, c.AuthorIP, "Comment's author IP");
                Assert.Less(c.Date, DateTime.Now, "Comment's date");
                Assert.Less(c.DateGMT, DateTime.Now, "Comment's dateGMT");
                Assert.AreEqual("Bonjour, ceci est un commentaire.<br /> Pour supprimer un commentaire, connectez-vous, et affichez les commentaires de cet article. Vous pourrez alors les modifier ou les supprimer.", c.Content, "Comment's content");
                Assert.AreEqual(0, c.Karma, "Comment's karma");
                Assert.AreEqual("1", c.Approved, "Comment's Approment");
                Assert.AreEqual(string.Empty, c.Agent, "Comment's agent");
                Assert.AreEqual(string.Empty, c.Type, "Comment's type");
                Assert.Throws<NHibernate.ObjectNotFoundException>(() => text = c.Parent.Content, "Comment's parent");
                Assert.Throws<NHibernate.ObjectNotFoundException>(() => text = c.User.Login, "Comment's parent");



            }

        }

        [Test]
        public void GetDefaultLinks()
        {
            // Arrange
            IList<Link> links;
            Link linkWithRss;

            // Act
            links = _repository.FindAll<Link>();
            linkWithRss = _repository.FindOne<Link>(2);

            // Assert
            Assert.AreEqual(7, links.Count);

            Assert.AreEqual(2, linkWithRss.Id, "link id");
            Assert.AreEqual("http://wordpress.org/development/", linkWithRss.Url, "link url");
            Assert.AreEqual("Development Blog", linkWithRss.Name, "link name");
            Assert.AreEqual("", linkWithRss.Image, "link image");
            Assert.AreEqual("", linkWithRss.Target, "link target");
            Assert.AreEqual("", linkWithRss.Description, "link description");
            Assert.AreEqual("Y", linkWithRss.Visible, "link visible");
            Assert.AreEqual("admin", linkWithRss.Owner.Login, "link owner");
            Assert.AreEqual(0, linkWithRss.Rating, "link rating");
            Assert.GreaterOrEqual(DateTime.MaxValue, linkWithRss.Updated, "link update datetime");
            Assert.LessOrEqual(DateTime.MinValue, linkWithRss.Updated, "link update datetime");
            Assert.AreEqual("http://wordpress.org/development/feed/", linkWithRss.Rss, "link rss");

        }

        public void GetDefaultOptions()
        {
            
        }

        [Test]
        public void GetDefaultUser()
        {
            IList<User> users = _repository.FindAll<User>();


            Assert.AreEqual(1, users.Count, "1 user in database");

        }
    }
}
